/*-----------------------------------------------------------------------------
	The contents of this file are subject to the Mozilla Public License
	Version 1.1 (the "License"); you may not use this file except in
	compliance with the License. You may obtain a copy of the License at
	http://www.mozilla.org/MPL/
	
	Software distributed under the License is distributed on an "AS IS"
	basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
	License for the specific language governing rights and limitations
	under the License.
	
	The Original Code is Fever Framework code.
	
	The Initial Developer of the Original Code is Romain Ecarnot.
	Portions created by Initial Developer are Copyright (C) 2006
	the Initial Developer. All Rights Reserved.
	
	Contributor(s):
	
	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
-----------------------------------------------------------------------------*/

import org.aswing.tree.TreePath;

import fvaswing.components.tree.FvAbstractTreeModel;
import fvaswing.components.tree.FvTreeNode;

/**
 * Default model to use with {@link fvaswing.components.FvTree} component.
 * 
 * <p>Extends {@link fvaswing.components.FvAbstractTreeModel} class to deploy 
 * strong typing strategy.
 * 
 * <p>Allow childnodes sorting.
 * 
 * @author Romain Ecarnot
 */
class fvaswing.components.tree.FvTreeModel extends FvAbstractTreeModel
{
	//-------------------------------------------------------------------------
	// Public API
	//-------------------------------------------------------------------------
	
	/**
      * Creates a tree specifying whether any node can have children,
      * or whether only certain nodes can have children.
      *
      * @param root a TreeNode object that is the root of the tree
      * @param asc (optional)a boolean, false if any node can
      *        have children, true if each node is asked to see if
      *        it can have children. Default is false.
      * @see #asksAllowsChildren
      */
    public function FvTreeModel( root : FvTreeNode, asc : Boolean )
    {
        super( root, asc );
    }
	
    /**
     * Returns the root of the tree.  Returns null only if the tree has
     * no nodes.
     *
     * @return  the root of the tree
     */
    public function getRoot() : FvTreeNode
    {
        return FvTreeNode( _rootNode );
    }
	
	/**
	 * Returns tree node resgitred with passed-in {@code nodeID} in 
	 * current tree.
	 * 
	 * <p>If not, return {@code null}
	 */
	public function getNodeById( nodeID : Number ) : FvTreeNode
	{
		var nodes : Array = getRoot().preorderEnumeration();
		var nbItem : Number = nodes.length;
		
		for( var i : Number = 0; i < nbItem; i++ ) 
		{
			var node : FvTreeNode = nodes[i];
			if( node.getID() == nodeID ) return node;
		}
		
		return null;
	}
	
	/**
      * This sets the user object of the TreeNode identified by path
      * and posts a node changed.  
      * 
      * <p>If you use custom user objects in the TreeModel you're going to 
      * need to subclass this and set the user object of the changed node 
      * to something meaningful.
      */
    public function valueForPathChanged( path : TreePath, newValue ) : Void
    {
		var aNode : FvTreeNode = FvTreeNode( path.getLastPathComponent() );
		
		aNode.getUserObject().setValue( newValue );
		nodeChanged( aNode );
    }
    
    /**
     * Returns the index of child in parent.
     * If either the parent or child is <code>null</code>, returns -1.
     * 
     * @param parent a note in the tree, obtained from this data source
     * @param child the node we are interested in
     * @return the index of the child in the parent, or -1
     *    if either the parent or the child is <code>null</code>
     */
    public function getIndexOfChild( parent : FvTreeNode, child : FvTreeNode ) : Number
    {
		return super.getIndexOfChild( parent, child );
    }

    /**
     * Returns the child of <I>parent</I> at index <I>index</I> in the parent's
     * child array.  <I>parent</I> must be a node previously obtained from
     * this data source. This should not return null if <i>index</i>
     * is a valid index for <i>parent</i> (that is <i>index</i> >= 0 &&
     * <i>index</i> < getChildCount(<i>parent</i>)).
     *
     * @param   parent  a node in the tree, obtained from this data source
     * @return  the child of <I>parent</I> at index <I>index</I>
     */
    public function getChild( parent : FvTreeNode, index : Number ) : FvTreeNode
    {
        return parent.getChildAt( index );
    }
	
    /**
     * Returns the number of children of <I>parent</I>.  Returns 0 if the node
     * is a leaf or if it has no children.  <I>parent</I> must be a node
     * previously obtained from this data source.
     *
     * @param   parent  a node in the tree, obtained from this data source
     * @return  the number of children of the node <I>parent</I>
     */
    public function getChildCount( parent : FvTreeNode ) : Number
    {
        return parent.getChildCount();
    }
	
	 /** 
     * Returns whether the specified node is a leaf node.
     * The way the test is performed depends on the
     * <code>askAllowsChildren</code> setting.
     *
     * @param node the node to check
     * @return true if the node is a leaf node
     */
    public function isLeaf( node : FvTreeNode ) : Boolean
    {
		return super.isLeaf( node );
    }
    
	/**
     * Invoked this to insert newChild at location index in parents children.
     * 
     * <p>This will then message nodesWereInserted to create the appropriate
     * event. 
     * 
     * <p>This is the preferred way to add children as it will create
     * the appropriate event.
     */
    public function insertNodeInto( newChild : FvTreeNode, parent : FvTreeNode, index : Number ) : Void
    {
        super.insertNodeInto( newChild, parent, index );
        
        if( _autoSortingEnabled ) sortNode( parent );
    }
    
	 /**
     * Message this to remove node from its parent. This will message
     * nodesWereRemoved to create the appropriate event. This is the
     * preferred way to remove a node as it handles the event creation
     * for you.
     */
    public function removeNodeFromParent( node : FvTreeNode ) : Void
    {
        super.removeNodeFromParent( node );
    }
    
    /**
      * Invoke this method after you've changed how node is to be
      * represented in the tree.
      */
    public function nodeChanged( node : FvTreeNode ) : Void
    {
       super.nodeChanged( node );
    }
    
    /**
     * Invoke this method if you've modified the TreeNodes upon which this
     * model depends.  The model will notify all of its listeners that the
     * model has changed below the node <code>node</code> (PENDING).
     * @param node (optional). Default is root.
     */
    public function reload( node : FvTreeNode ) : Void
    {
    	super.reload( node );
    }
    
    /**
	 * Sorts passed-in <code>node</code> childnodes.
	 */
	public function sortNode( node : FvTreeNode ) : Void
	{
		super.sortNode( node );
	}
	
    /**
      * Invoke this method after you've inserted some TreeNodes into
      * node.  childIndices should be the index of the new elements and
      * must be sorted in ascending order.
      */
    public function nodesWereInserted( node : FvTreeNode, childIndices : Array ) : Void
    {
       super.nodesWereInserted( node, childIndices );
    }
	
	/**
      * Invoke this method after you've removed some TreeNodes from
      * node.  childIndices should be the index of the removed elements and
      * must be sorted in ascending order. And removedChildren should be
      * the array of the children objects that were removed.
      */
    public function nodesWereRemoved( node : FvTreeNode, childIndices : Array, removedChildren : Array ) : Void
    {
		super.nodesWereRemoved( node, childIndices, removedChildren );
    }
     
	/**
	 * Invoke this method after you've changed how the children identified by
	 * childIndicies are to be represented in the tree.
	 */
    public function nodesChanged( node : FvTreeNode, childIndices : Array ) : Void
    {
    	super.nodesChanged( node, childIndices );
    }
    
    /**
      * Invoke this method if you've totally changed the children of
      * node and its childrens children.
      * 
      * <p>This will post a treeStructureChanged event.
      */
    public function nodeStructureChanged( node : FvTreeNode ) : Void
    {
    	super.nodeStructureChanged( node );	
    }
}